package com.zhoug.zmui3demo.ui;

import android.os.Bundle;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;

import com.zhoug.common3.utils.TimeUtils;
import com.zhoug.zmui3.date.ZMUIDatePicker;
import com.zhoug.zmui3.dialog.date.ZMUIDateChooser;
import com.zhoug.zmui3demo.R;

import java.util.Date;

import androidx.appcompat.app.AppCompatActivity;

public class ZMUIDatePickerActivity extends AppCompatActivity {
    private boolean showDay = true;
    private int mode = ZMUIDatePicker.MODE_DATE | ZMUIDatePicker.MODE_TIME;
    private Button mBtn1;
    private Button mBtn2;
    private ZMUIDatePicker mDatePicker;
    private ZMUIDatePicker mDatePicker2;
    private TextView mTvDate;
    private Button mBtnMode;
    private Switch mSwitchIn;
    private Switch mSwitchYMD;
    private Switch mSwitchHM;
    private Button mBtnYearMonth;
    private Button mBtnYear;
    private ZMUIDatePicker mDatePickerYear;
    private ZMUIDatePicker mDatePickerYearMonth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_z_m_u_i_date_picker);
        findViews();
        init();
//        DatePicker
    }

    private void findViews() {
        mBtn1 = findViewById(R.id.btn1);
        mBtn2 = findViewById(R.id.btn2);
        mDatePicker = findViewById(R.id.datePicker);
        mDatePicker2 = findViewById(R.id.datePicker2);
        mTvDate = findViewById(R.id.tv_date);
        mBtnMode = findViewById(R.id.btn_mode);
        mSwitchIn = findViewById(R.id.switchInt);
        mBtnYearMonth = findViewById(R.id.btn_year_month);
        mBtnYear = findViewById(R.id.btn_year);
        mDatePickerYear = findViewById(R.id.datePickerYear);
        mDatePickerYearMonth = findViewById(R.id.datePickerYearMonth);
    }

    private void init() {
        mBtnMode.setOnClickListener(v -> {
            if (mode == ZMUIDatePicker.MODE_DATE_TIME) {
                mode = ZMUIDatePicker.MODE_DATE;
                mBtnMode.setText("选择日期");
            } else if (mode == ZMUIDatePicker.MODE_DATE) {
                mode = ZMUIDatePicker.MODE_TIME;
                mBtnMode.setText("选择时间");
            } else {
                mode = ZMUIDatePicker.MODE_DATE_TIME;
                mBtnMode.setText("选择日期+时间");
            }
            setMode(mDatePicker);
            setMode(mDatePicker2);
        });

        mSwitchIn.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDatePicker.setSuffixPosition(ZMUIDatePicker.SUFFIX_POSITION_IN);
                mDatePicker2.setSuffixPosition(ZMUIDatePicker.SUFFIX_POSITION_IN);
            } else {
                mDatePicker.setSuffixPosition(ZMUIDatePicker.SUFFIX_POSITION_OUT);
                mDatePicker2.setSuffixPosition(ZMUIDatePicker.SUFFIX_POSITION_OUT);
            }
            mDatePicker.notifyDataChange();
            mDatePicker2.notifyDataChange();


        });

        mBtn1.setOnClickListener(v -> {
            ZMUIDateChooser zmuiDateChooser = new ZMUIDateChooser(this);
            zmuiDateChooser.setMaxDate(new Date());
            zmuiDateChooser.showDay(showDay);
            zmuiDateChooser.setType(showDay ? ZMUIDateChooser.TYPE_DATE_TIME : ZMUIDateChooser.TYPE_DATE);
            zmuiDateChooser.setCallback((year, month, day, hour, minute) -> {
                zmuiDateChooser.dismiss();
                if (showDay) {
                    setDate(year + "-" + month + "-" + day + " " + hour + ":" + minute);
                } else {
                    setDate(year + "-" + month);
                }
            });
            zmuiDateChooser.show();

        });
        mBtn2.setOnClickListener(v -> setDate(mDatePicker.getDateString() + "\n" + mDatePicker2.getDateString()));

        mBtnYear.setOnClickListener(v -> {
            ZMUIDateChooser zmuiDateChooser = new ZMUIDateChooser(this);
            zmuiDateChooser.setMaxDate(new Date());
            zmuiDateChooser.showMonth(false);
            zmuiDateChooser.showDay(false);
            zmuiDateChooser.setType(ZMUIDateChooser.TYPE_DATE);
            zmuiDateChooser.setCallback((year, month, day, hour, minute) -> {
                zmuiDateChooser.dismiss();
                setDate(year);
            });
            zmuiDateChooser.show();
        });
        mBtnYearMonth.setOnClickListener(v -> {
            ZMUIDateChooser zmuiDateChooser = new ZMUIDateChooser(this);
            zmuiDateChooser.setMaxDate(new Date());
            zmuiDateChooser.showMonth(true);
            zmuiDateChooser.showDay(false);
            zmuiDateChooser.setType(ZMUIDateChooser.TYPE_DATE);
            zmuiDateChooser.setCallback((year, month, day, hour, minute) -> {
                zmuiDateChooser.dismiss();
                setDate(year + "-" + month);
            });
            zmuiDateChooser.show();
        });

        configDatePicker(mDatePicker);
        configDatePicker(mDatePicker2);

        mDatePickerYear.setMode(ZMUIDatePicker.MODE_DATE);
        mDatePickerYear.setSuffixPosition(ZMUIDatePicker.SUFFIX_POSITION_IN);
        mDatePickerYear.showMonth(false);
        mDatePickerYear.showDay(false);
        mDatePickerYear.notifyDataChange();
        mDatePickerYear.setOnSelectChangeListener(new ZMUIDatePicker.OnSelectChangeListener() {
            @Override
            public void onDateChange(int year, int month, int day, int hour, int minute) {
                setDate(year+"");
            }

            @Override
            public void onDateChange(String date) {

            }
        });

        mDatePickerYearMonth.setMode(ZMUIDatePicker.MODE_DATE);
        mDatePickerYearMonth.setSuffixPosition(ZMUIDatePicker.SUFFIX_POSITION_IN);
        mDatePickerYearMonth.showMonth(true);
        mDatePickerYearMonth.showDay(false);
        mDatePickerYearMonth.notifyDataChange();
        mDatePickerYearMonth.setOnSelectChangeListener(new ZMUIDatePicker.OnSelectChangeListener() {
            @Override
            public void onDateChange(int year, int month, int day, int hour, int minute) {
                setDate(year+"-"+month);
            }

            @Override
            public void onDateChange(String date) {

            }
        });
    }

    private void configDatePicker(ZMUIDatePicker datePicker) {
        datePicker.setMode(mode);
//        mDatePicker.setMode(ZMUIDatePicker.MODE_DATE );
//        mDatePicker.setMode( ZMUIDatePicker.MODE_TIME );
//        mDatePicker.setSelectYear(2029);
//        mDatePicker.setSelectMonth(22);
//        mDatePicker.setSelectDay(55);
//        mDatePicker.setSelectHour(22);
//        mDatePicker.setSelectMinute(56);
        datePicker.setSelect(TimeUtils.parse("2020-05-01 13:10", "yyyy-MM-dd HH:mm").getTime());
        datePicker.setMinDate(TimeUtils.parse("2020-02-15 12:30", "yyyy-MM-dd HH:mm").getTime());
        datePicker.setMaxDate(TimeUtils.parse("2025-08-25 22:10", "yyyy-MM-dd HH:mm").getTime());

        datePicker.setSuffixPosition(ZMUIDatePicker.SUFFIX_POSITION_IN);
        datePicker.notifyDataChange();
    }

    private void setMode(ZMUIDatePicker datePicker) {
        if (mode == ZMUIDatePicker.MODE_TIME) {
            datePicker.setMode(ZMUIDatePicker.MODE_TIME);
            datePicker.setDateFormat("HH:mm");
        } else if (mode == ZMUIDatePicker.MODE_DATE) {
            datePicker.setMode(ZMUIDatePicker.MODE_DATE);
            datePicker.setDateFormat("yyyy-MM-dd");
        } else {
            datePicker.setMode(ZMUIDatePicker.MODE_DATE | ZMUIDatePicker.MODE_TIME);
            datePicker.setDateFormat("yyyy-MM-dd HH:mm");
        }

        datePicker.notifyDataChange();
    }

    private void setDate(String date) {
        mTvDate.setText(date);
    }
}